﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ALinq.BBS.DataAccess
{
    public class Boards : IEnumerable<Board>, System.IDisposable
    {
        private IQueryable<Board> source;
        private Board root;
        private List<Board> items;
        private Database dataContext;
        //private Board parent;

        public Boards()
        {
            //this.parent = parent;
        }

        IEnumerable<Board> Source
        {
            get
            {
                if (items == null)
                {
                    items = DataContext.Boards.ToList();
                }
                return items;
            }
        }

        Database DataContext
        {
            get
            {
                if (dataContext == null)
                {
                    dataContext = new Database();
                    var ds = new DataLoadOptions();
                    ds.LoadWith<Board>(o => o.Forums);

                    dataContext.LoadOptions = ds;
                }

                return dataContext;
            }
        }

        IEnumerator<Board> IEnumerable<Board>.GetEnumerator()
        {
            return Source.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Source.GetEnumerator();
        }

        public Board AddItem(string name)
        {
            var id = DataContext.Boards.Max(o => o.ID) + 1;
            var item = new Board
            {
                ID = id,
                Name = name,
                DataContext = DataContext
            };

            DataContext.Boards.Insert(item);

            if (items != null)
                items.Add(item);

            return item;
        }

        public void UpdateItem(int boardID, string boardName)
        {


            DataContext.Boards.Update(o => new Board { Name = boardName }, o => o.ID == boardID);

            if (items != null)
            {
                var item = items.SingleOrDefault(o => o.ID == boardID);
                if (item == null)
                    return;
                item.Name = boardName;
            }

        }

        public void Clear()
        {
            DataContext.Boards.Delete(o => true);

            if (items != null)
                items.Clear();
        }

        public void DeleteItem(int id)
        {
            DataContext.Boards.Delete(o => o.ID == id);

            if (items != null)
            {
                var item = items.SingleOrDefault(o => o.ID == id);
                if (item != null)
                    items.Remove(item);
            }
        }

        public void DeleteItems(IEnumerable<int> ids)
        {
            DataContext.Boards.Delete(o => ids.Contains(o.ID));

            if (items != null)
            {
                var lists = items.Where(o => ids.Contains(o.ID)).ToList();
                //if (item != null)
                //    items.Remove(item);
                lists.ForEach(o => items.Remove(o));
            }
        }

        void IDisposable.Dispose()
        {
            if (dataContext != null)
                dataContext.Dispose();
        }


    }
}
